package net.airtheva.uniqueTask5;

import android.content.Context;
import android.util.AttributeSet;
import android.util.Log;
import android.widget.AbsListView;
import android.widget.ListView;

public class OverScrollListView extends ListView implements AbsListView.OnScrollListener {

	public static final String TAG = "OverScrollListView";
	
	public interface OnOverScrollListener {
		void OnOverScrolling(int deltaY);
		void OnOverScrolled();
	}

	OnOverScrollListener mOnOverScrollListener;

	public OverScrollListView(Context context, AttributeSet attrs) {
		super(context, attrs);
		// TODO Auto-generated constructor stub
		Log.d(TAG, "initialize 2.");
		setOnScrollListener(this);
	}

	public OverScrollListView(Context context, AttributeSet attrs, int defStyle) {
		super(context, attrs, defStyle);
		// TODO Auto-generated constructor stub
		Log.d(TAG, "initialize 3.");
		setOnScrollListener(this);
	}

	@Override
	protected boolean overScrollBy(int deltaX, int deltaY, int scrollX, int scrollY, int scrollRangeX, int scrollRangeY, int maxOverScrollX, int maxOverScrollY, boolean isTouchEvent) {
		if(mOnOverScrollListener != null) {
			mOnOverScrollListener.OnOverScrolling(- deltaY);
		}
		return super.overScrollBy(deltaX, deltaY, scrollX, scrollY, scrollRangeX, scrollRangeY, maxOverScrollX, maxOverScrollY, isTouchEvent);
	}

	@Override
	public void onScroll(AbsListView view, int firstVisibleItem, int visibleItemCount, int totalItemCount) {
		Log.d(TAG, "onScroll.");
		if(mOnOverScrollListener != null) {
			mOnOverScrollListener.OnOverScrolled();
		}
	}

	@Override
	public void onScrollStateChanged(AbsListView view, int scrollState) {
		Log.d(TAG, "onScrollStateChanged.");
		if(scrollState == SCROLL_STATE_IDLE || scrollState == SCROLL_STATE_FLING) {
			if(mOnOverScrollListener != null) {
				mOnOverScrollListener.OnOverScrolled();
			}
		}
	}

	public void SetOnOverScrollListener(OnOverScrollListener l) {
		mOnOverScrollListener = l;
	}

}
